#include "tp2.h"
#include <stdio.h>
#include <stdlib.h>
#include <rpc/pmap_clnt.h>
#include <string.h>
#include <memory.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <my_global.h>
#include <mysql.h>

MYSQL *myconn;

static void tp2_prog_1(struct svc_req *rqstp, register SVCXPRT * transp) {
	union {
		char *query_1_arg;
	} argument;
	union {
		char *query_1_res;
	} result;
	bool_t retval;
	xdrproc_t _xdr_argument, _xdr_result;

	bool_t(*local) (char *, void *, struct svc_req *);

	switch (rqstp->rq_proc) {
	case NULLPROC:
		(void) svc_sendreply(transp, (xdrproc_t) xdr_void, (char *) NULL);
		return;

	case QUERY:
		_xdr_argument = (xdrproc_t) xdr_wrapstring;
		_xdr_result = (xdrproc_t) xdr_wrapstring;
		local = (bool_t(*)(char *, void *, struct svc_req *)) query_1_svc;
		break;

	default:
		svcerr_noproc(transp);
		return;
	}
	memset((char *) &argument, 0, sizeof(argument));
	if (!svc_getargs(transp, (xdrproc_t) _xdr_argument, (caddr_t) & argument)) {
		svcerr_decode(transp);
		return;
	}
	retval = (bool_t) (*local) ((char *) &argument, (void *) &result, rqstp);
	if (retval > 0 && !svc_sendreply(transp, (xdrproc_t) _xdr_result, (char *) &result)) {
		svcerr_systemerr(transp);
	}
	if (!svc_freeargs(transp, (xdrproc_t) _xdr_argument, (caddr_t) & argument)) {
		fprintf(stderr, "%s", "unable to free arguments");
		exit(1);
	}
	if (!tp2_prog_1_freeresult(transp, _xdr_result, (caddr_t) & result))
		fprintf(stderr, "%s", "unable to free results");

	return;
}

int main() {
	register SVCXPRT *transp;

	/* Criando MySQL Handle */
	myconn = mysql_init(NULL);
	printf("Conectando ao banco de dados... ");
	if (mysql_real_connect(myconn, "localhost", "grupo03sd", "9bf5be", "grupo03sd", 0, NULL, 0) ==
		NULL) {
		fprintf(stderr, "erro de conexao com o banco.\n");
		exit(1);
	}
	printf("[OK]\n");
	printf("Limpando a tabela 'funcionarios' do banco... ");
	if (mysql_query(myconn, "DELETE FROM funcionarios;") != 0) {
		fprintf(stderr, "falha ao limpar a tabela!!\n");
		exit(1);
	}
	printf("[OK]\n");

	printf("Servidor inicializado e aguardando conexões.\n\n");

	pmap_unset(TP2_PROG, TP2_VERS);

	transp = svcudp_create(RPC_ANYSOCK);
	if (transp == NULL) {
		fprintf(stderr, "%s", "cannot create udp service.");
		exit(1);
	}
	if (!svc_register(transp, TP2_PROG, TP2_VERS, tp2_prog_1, IPPROTO_UDP)) {
		fprintf(stderr, "%s", "unable to register (TP2_PROG, TP2_VERS, udp).");
		exit(1);
	}

	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
	if (transp == NULL) {
		fprintf(stderr, "%s", "cannot create tcp service.");
		exit(1);
	}
	if (!svc_register(transp, TP2_PROG, TP2_VERS, tp2_prog_1, IPPROTO_TCP)) {
		fprintf(stderr, "%s", "unable to register (TP2_PROG, TP2_VERS, tcp).");
		exit(1);
	}

	svc_run();
	fprintf(stderr, "%s", "svc_run returned");
	exit(1);
	/* NOTREACHED */
}
